/*
 * $Id: dictc.h 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
 *
 * Copyright (c) 2006, 2007, 2008 Prabhu Gurumurthy <pgurumur@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef __DICTC_H_
#define __DICTC_H_

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/utsname.h>

#include <netinet/in.h>

#include <net/if.h>

#include <arpa/inet.h>

#include <assert.h>
#ifdef __NetBSD__
#include <ctype.h>
#endif
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <histedit.h>
#include <netdb.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <readline/readline.h>
#include <readline/history.h>

#define DICT_PORT 2628
#define CMDLINE_LENGTH 1024
#define CMDLINE_BUF 8192
#define MAX_CMDLINE CMDLINE_LENGTH - 2
#define MAXLINE 4096
#define RESCODE_LENGTH 4
#define MAXSLEEP 1
#define MAXTOKEN 7

#define DICT_CLIENT_INFO "client info OpenBSD 4.0 version 0.1\r\n"

#ifdef __linux__
#define strlcpy strncpy
#endif

typedef enum {
   FALSE = 0,
   TRUE = 1
} boolean;

struct dict_client {
   struct sockaddr_in dsock4_info;
   struct sockaddr_in6 dsock6_info;
   char *serv_name;
   int af;
   int serv_port;
   int sockfd;
   int conn;
};

struct dict_cmd {
   char *cmd_name;
   void (*cmd_handler)(int, char*);
};

__BEGIN_DECLS

int main(int, char *[]);
void usage(void);
void create_socket(void);
void do_prompt(void);
void cleanup(void);
char *get_prompt(void);
void parse_commands(char *);
void write_command(const char *);
void print_history(void);
void print_help(void);

/* io functions */
void set_nonblock(void);
void unset_nonblock(void);
void sock_read(void);
void sock_write(const char *);

/* signal functions */
void handle_signal(int);

/* error printing functions */
void error(const char *, ...);
void warning(const char *, ...);

/* command subroutines */
void cmd_define(int, char *);
void cmd_match(int, char *);
void cmd_show(int, char *);
void cmd_option(int, char *);
void cmd_client(int, char *);
void cmd_auth(int, char *);
void cmd_status(int, char *);
void cmd_help(int, char *);
void cmd_quit(int, char *);

__END_DECLS

extern char *__progname;

#endif

